<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\Wqdocument;
use think\Db;
class Wqdoc extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Wqdocument');
        $typeList = Wqdocument::getTypeList();
        $this->view->assign("typeList", $typeList);
        $this->assignconfig('typeList', $typeList);
    }
    
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
 
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
                        
            $result = ['total' => $list->total(), 'rows' => $list->items()];
            return json($result);
        }
        return $this->view->fetch();
    }
    
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            Db::startTrans();
            try {
                $params['is_audit'] = 1;
                $document = model('Document')->create($params,true);
                $params['document_id'] = $document->id;
                $row = $this->model->create($params,true);
                $document = $row->toArray();

                unset($document['id']);
                unset($document['status_text']);
                
                
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            Db::name('Document')->where('id',$row->document_id)->update(['doc_id' => $row['id']]);
            $this->success();
        }
        return $this->view->fetch();
    }
    
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            Db::startTrans();
            try {
  
                $row->allowField(true)->save($params);
                $document = $row->toArray();
                $document['doc_id'] = $document['id'];
                unset($document['id']);
                unset($document['status_text']);
                unset($document['document_id']);
                Db::name('Document')->where('id', $row['document_id'])->update($document);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }

            $this->success();
        }
        
   
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }
    
    public function del($ids = '')
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            foreach ($list as $k => $v) {
                $count += $v->delete();
            }
            if ($count) {
                $this->success();
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
    
    public function multi($ids = '')
    {
        $ids = $ids ? $ids : $this->request->param('ids');
        if ($ids) {
            if ($this->request->has('params')) {
                parse_str($this->request->post('params'), $values);
                $values = array_intersect_key($values, array_flip(['status', 'flag']));
                if ($values) {
                    $count = $this->model->where('id', 'in', $ids)->update($values);
                    if ($count) {
                        $this->success();
                    }
                }
                $this->error(__('Operation failed'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
}